# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=postgresql-pg_graphql
_projname=pg_graphql
pkgver=1.4.2
pkgrel=0
pkgdesc="GraphQL support for PostgreSQL"
url="https://supabase.github.io/pg_graphql/"
# ppc64le,riscv64,s390x: fails to build due to ring crate (https://github.com/pgcentralfoundation/pgrx/pull/1442)
# loongarch64: fails to build cargo-pgrx
arch="all !ppc64le !riscv64 !s390x !loongarch64"
license="Apache-2.0"
makedepends="
	cargo
	chrpath
	jq
	openssl-dev
	postgresql
	postgresql-dev
	rustfmt
	"
source="https://github.com/supabase/pg_graphql/archive/v$pkgver/$_projname-$pkgver.tar.gz
	remove-regex.patch
	"
builddir="$srcdir/$_projname-$pkgver"
# check: tests tries to write to /usr/lib/...
# net: fetches crates in build phase
options="!check net"

export PGRX_HOME="$srcdir/.pgrx"

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	local pgver="$(pg_config --major-version)"

	# We must install it this way because cargo-pgrx and pgrx dependency
	# versions in the project must be identical. :(
	local pgrx_ver; pgrx_ver="$(cargo metadata --locked --format-version 1 \
		| jq -r '.packages[] | select(.name == "pgrx") | .version')"
	# Use dev profile to speed-up compilation.
	cargo install --locked --profile dev --bins --root target/tools "cargo-pgrx@$pgrx_ver"

	export PATH="$PWD/target/tools/bin:$PATH"

	cargo pgrx init --pg$pgver="$(which pg_config)"

	# TODO: Remove after >0.11.2 is released (https://github.com/pgcentralfoundation/pgrx/pull/1441).
	[ -n "$RUSTFLAGS" ] && export RUSTFLAGS="$(echo $RUSTFLAGS)" || unset RUSTFLAGS

	cargo pgrx package --verbose --out-dir="target/release/dist"

	# Remove RPATH.
	# NOTE: CARGO_PROFILE_RELEASE_RPATH=false doesn't work here.
	chrpath -d target/release/dist/usr/lib/postgresql*/*.so
}

package() {
	depends="postgresql$(pg_config --major-version)"

	mkdir -p "$pkgdir"
	cp -r target/release/dist/* "$pkgdir"/
}

sha512sums="
d1d0612b39916062b85b855975948376aae47066028532c118ee404808c245fc7617fd0a39a410397572601194f38f7a04a69a99b8aa71f43a79cc8710223cb4  pg_graphql-1.4.2.tar.gz
dc517bfb9813a1c807c01146d7f81dbfc14b004fb5be915794092bf7a2cdfa84edcdfe221094f8e196cf169980c02cdfcf339d80fd16114c040a03a3d765b416  remove-regex.patch
"
